let msg = require('../../util/msg.js');
let Card = require('./card.js');
let Poker = require('./poker.js');
let save = require('../save.js');
let GameState = require('./status.js');
let log = require('../../util/log.js');
let gameData = require('../../util/gameData.js');

module.exports = DDZ;

let enumStatus = GameState.statusID;

function DDZ(tableid) {
    this._event = [];       // 事件注册表
    this._gameLv = null;    // 房间类型
    this._players = [];		// 保存玩家信息
    this._askInfo = [];		// 叫分数据
    this._history = [];     // 出牌记录
    this._holes = null;     // 底牌
    this._dzList = null;	// 抢地主的人选
    this._dzID = null;			// 地主id
    this._baseScore = 1;	// 底分
    this._multiple = 0;     // 倍数
    this._previousID = 0;		// 之前出牌的玩家'
    this._playID = 0;		// 当前操作的玩家'
    this._ticket = 0;        // 房费
    this._winCap = 0;       // 封顶
    this._firstSC = null;

    this._tableid = tableid;
    this._lossTime = 0;     // 流局次数
    this._state = GameState.createState(enumStatus.Idle);
}
DDZ.prototype.setEventHandle = function (handler) {
    //log.d("set event handler:", this, handler)
    this._eventHandler = handler;
};
DDZ.prototype.emit = function (name) {

    if (this._eventHandler == null) {
        log.d('event emit: ' + name, this);
        return;
    }
    let handler = this._eventHandler[name];
    if (handler != null) {
        arguments[0] = this;
        handler.apply(this._eventHandler, arguments);
    }
};
DDZ.prototype.getGameLv = function () {
    return this._gameLv;
};
DDZ.prototype.getName = function () {
    return 'ddz table';
};
DDZ.prototype.getTableID = function () {
    return this._tableid;
};
DDZ.prototype.getStateID = function () {
    return this._state.id;
};
// 返回底牌
DDZ.prototype.getHoleCards = function () {
    return this._holes;
};
DDZ.prototype.setGameLv = function (gameLv) {
    this._gameLv = gameLv;
};
DDZ.prototype.setBaseScore = function (scoreSize) {
    this._baseScore = scoreSize;
};
DDZ.prototype.getBaseScore = function () {
    return this._baseScore;
};
DDZ.prototype.getMultiple = function () {
    return this._multiple;
};
DDZ.prototype.setTicket = function (ticket) {
    this._ticket = ticket;
};
DDZ.prototype.getTicket = function () {
    return this._ticket;
};
DDZ.prototype.setWinCap = function (winCap) {
    this._winCap = winCap;
};
DDZ.prototype.getWinCap = function () {
    return this._winCap;
};
DDZ.prototype.getFree = function () {
    return 1;
};
DDZ.prototype.getRoomId = function () {
    return 1;
};
DDZ.prototype.getPlayID = function () {
    return this._playID;
};
DDZ.prototype.getMultiples = function () {
    let multiples = {};
    if (this._dzID == null) {
        multiples[0] = this._multiple * this.getPlayer(0).getDouble();
        multiples[1] = this._multiple * this.getPlayer(1).getDouble();
        multiples[2] = this._multiple * this.getPlayer(2).getDouble();
    } else {
        multiples[(this._dzID + 1) % 3] = this._multiple * this.getPlayer((this._dzID + 1) % 3).getDouble();
        multiples[(this._dzID + 2) % 3] = this._multiple * this.getPlayer((this._dzID + 2) % 3).getDouble();
        multiples[this._dzID] = this._multiple * (this.getPlayer((this._dzID + 1) % 3).getDouble() + this.getPlayer((this._dzID + 2) % 3).getDouble());
    }
    return multiples;
};
DDZ.prototype.getNext = function () {
    if (this._dzList != null)
        return this._dzList[this._playID];
    else
        return this._playID;
};
DDZ.prototype.getDZ = function () {
    return this._dzID;
};
DDZ.prototype.getHistory = function () {
    return this._history;
};
DDZ.prototype.getLastHands = function () {
    return this._lastHands;
};
DDZ.prototype.getPlayer = function (seat) {
    ////log.d('get seat', seat, this._players[seat]._uuid);
    return this._players[seat];
};
DDZ.prototype.getLossTime = function () {
    return this._lossTime;
};
DDZ.prototype.getCurrentPlayer = function () {
    return this._players[this._playID];
};
DDZ.prototype.getCurrentRobPlayer = function () {
    return this._players[this._dzList[this._playID]];
};
DDZ.prototype.getPreviousID = function () {
    return this._previousID;
};
DDZ.prototype.statusIsPlaying = function () {
    if (this._state.id == GameState.statusID.Playing) {
        return true;
    }
    return false;
};
DDZ.prototype.setState = function (stateId, param) {
    process.nextTick(function (table) {
        table.removeTimeout();
        // 设置新状态.
        if (table._state != null) {
            table._state.onExit(table);
        }
        setTimeout(function () {
            table._state = GameState.createState(stateId);
            log.d('设置状态:', GameState.getStateName(stateId));
            table._state.onEnter(table, param);
        }, 1000);
    }, this);
};
DDZ.prototype.nextStateId = function () {
    switch (gameData.d.gameLv[this.getGameLv()].type) {
        case 'happy':
        case 'arena':
        case 'custom':
            switch (this.getStateID()) {
                case enumStatus.Idle:
                case enumStatus.Loss:
                    return enumStatus.Deal;
                case enumStatus.Deal:
                    return enumStatus.Ask;
                case enumStatus.Ask:
                    return enumStatus.Grab;
                case enumStatus.Grab:
                    return enumStatus.Double;
                case enumStatus.Double:
                    return enumStatus.Playing;
                case enumStatus.Playing:
                    return enumStatus.Over;
            }
            break;
        case 'match':
            switch (this.getStateID()) {
                case enumStatus.Idle:
                case enumStatus.Loss:
                    return enumStatus.Bid;
                case enumStatus.Bid:
                    return enumStatus.Playing;
                case enumStatus.Playing:
                    return enumStatus.Over;
            }
            break;
    }
};
DDZ.prototype.start = function (p1, p2, p3, cb) {
    this._players[0] = p1;
    this._players[1] = p2;
    this._players[2] = p3;
    this.onStart = cb;
    this.restart();
};
DDZ.prototype.restart = function () {
    this._timer = [];       // 超时对象
    this._managed = [];     // 托管队列
    this._askInfo = [];		  // 叫分数据
    this._history = [];     // 出牌记录
    this._dzID = null;			    // 地主id
    this._baseScore = 1;	  // 底分
    this._playID = 0;       // 当前操作玩家ID
    this._multiple = 0;     // 倍数
    this._firstSC = null;

    this.deal();
    //log.d(enumStatus);
    // 设置为叫分状态
    this.setState(this.nextStateId());
};
DDZ.prototype.canNotLoss = function () {
    return false;
};
DDZ.prototype.addLossTime = function () {
    this._lossTime++;
};

DDZ.prototype.setTimeout = function (callback, delay) {
    this.removeTimeout();
    // 15秒超时后, 自动放弃
    this._timer = setTimeout(function (ddz) {
        ddz._timer = null;
        callback();
    }, delay * 1000, this);
};
DDZ.prototype.removeTimeout = function () {
    if (this._timer != null) {
        clearTimeout(this._timer);
        //clearImmediate(this._timer);
        this._timer = null;
    }
};
DDZ.prototype.getTimeoutTime = function () {
    if (this._timer != null) {
        let ct = process.uptime() * 1000;
        let dt = ct - this._timer._idleStart;
        return this._timer._idleTimeout - dt;
    }

    return 0;
};
DDZ.prototype.getTimeoutTimeStamp = function () {
    if (this._timer != null) {
        let ct = process.uptime() * 1000;
        let dt = ct - this._timer._idleStart;
        let et = this._timer._idleTimeout - dt;
        return (Date.now() + et);
    }

    return 0;
};
// 叫分, score为倍数 0表示不要, 3表示叫3分, 4表示枪地主
// 返回: 成功返回true, 失败返回false
DDZ.prototype.askScore = function (seatId, score) {
    log.d('[' + this._tableid + '][' + seatId + ']叫分: ' + score);
    if (this.getStateID() != enumStatus.Bid)
        return false;
    // 判断是否应该由当前玩家操作
    if (seatId != this._playID) {
        log.d('应该由当前玩家操作', this._playID);
        return false;
    }

    // let player = this._players[seatId];
    // if(player==null)
    // 	return false;
    // 要记录下哪些人叫了地主, 以便后面抢地主使用
    if (score > 0) {
        if (this._multiple > 0 && score <= this._multiple) {
            log.d('叫分不能低于上一个人', this._playID);
            return false;
        }
        this._dzID = this._playID;
        this._multiple = score;
    }

    this._askInfo.push({'id': seatId, 'score': score});

    if (score >= 3) {
        this.setState(this.nextStateId());
        this.sendAskChip(seatId, score, true);
        return true;
    }
    else if (this._askInfo.length == 3) {
        if (this._multiple != 0) {
            this.setState(this.nextStateId());
            this.sendAskChip(seatId, score, true);
            return true;
        }
        if (this.canNotLoss()) {
            // 不能流局
            // 强制第一个可以叫地主的玩家1分叫地主
            this._dzID = this._playID;
            this._multiple = score;
            this.setState(this.nextStateId());
            this.sendAskChip(seatId, score, true);
            return true;
        }
        // 设置流局,流局后会重新发牌
        this.sendAskChip(seatId, score, true);
        this.setState(enumStatus.Loss, 'askScore');
        return true;
    }
    this._playID = (this._playID + 1) % 3;
    this._state.setTimeout(this);
    this._players[seatId].setScore(score);

    this.sendAskChip(seatId, score);
    return true;
};
/**
 * 长期流局随机一个人叫1分
 * @returns {boolean}
 */
DDZ.prototype.randAskScore = function () {
    this._dzID = Math.floor(Math.random() * 3);
    this._multiple = 1;
    this.setState(enumStatus.Playing);
    return true;
};
// 叫地主, score为倍数 0表示不叫, 1表示叫
// 返回: 成功返回true, 失败返回false
DDZ.prototype.askDZ = function (seatId, score) {
    log.d('[' + this._tableid + '][' + seatId + ']叫地主: ' + score);
    if (this.getStateID() != enumStatus.Ask)
        return false;
    // 判断是否应该由当前玩家操作
    if (seatId != this._playID) {
        log.d('应该由当前玩家操作', this._playID);
        return false;
    }

    if (score > 0) {
        this._multiple = this._multiple * 2;
        this.setState(this.nextStateId());
        this.sendAskDZ(seatId, score, true);
        return true;
    }
    this._askInfo.push(seatId);
    if (this._askInfo.length == 3) {
        if (this._firstSC !== null) {
            this._multiple = this._multiple * 2;
            this.setState(this.nextStateId());
            this.sendAskDZ(this._firstSC, score, true);
            return true;
        }
        // 设置流局,流局后会重新发牌
        this.sendAskDZ(seatId, score, true);
        this.setState(enumStatus.Loss, 'askDZ');
        return true;
    }
    this._playID = (this._playID + 1) % 3;
    this._state.setTimeout(this);

    this.sendAskDZ(seatId, score);
    return true;
};
/**
 * 长期流局随机一个人叫1分
 * @returns {boolean}
 */
DDZ.prototype.randAskDZ = function () {
    this._dzID = Math.floor(Math.random() * 3);
    this._multiple = this._multiple * 2;
    this.setState(enumStatus.Playing);
    return true;
};
// 抢地主
// 返回: 成功返回true,失败返回false
DDZ.prototype.robDZ = function (seatId, dz) {
    if (seatId == null || this.getStateID() != enumStatus.Grab)
        return false;
    let n = this._dzList.length;
    let id = this._dzList[this._playID];
    if (seatId != id) {
        log.d('应该由当前玩家[' + id + ']抢地主');
        return false;
    }
    this._playID = (this._playID + 1) % n;
    this._askInfo.push({'id': id, 'dz': parseInt(dz)});
    if (dz > 0) {
        this._dzID = id;
        this._multiple *= 2;
        log.d(id + '号抢地主. 当前底分:' + this._multiple);
    }
    if (this._askInfo.length == n) {
        this.sendRobDZ(seatId, dz, true);
        this.setState(this.nextStateId());
        return true;
    } else if (this._askInfo.length == (n - 1)) {
        let otherDZ = false;
        for (let i in this._askInfo) {
            if (this._askInfo[i].dz) {
                otherDZ = true;
            }
        }
        if (!otherDZ) {
            this.sendRobDZ(seatId, dz, true);
            this.setState(this.nextStateId());
            return true;
        }
    }

    this._state.setTimeout(this);
    this.sendRobDZ(seatId, dz);
    return true;
};
DDZ.prototype.double = function (seatId, double) {
    if (seatId == null || this.getStateID() != enumStatus.Double) {
        console.log('double false',1);
        return false;
    }

    if (this._askInfo.indexOf(seatId) >= 0) {
        return false;
    }
    this._askInfo.push(seatId);

    if (double > 0) {
        let isScore = false;
        if (gameData.d.gameLv[this.getGameLv()].coinType === 'score') {
            isScore = true;
        }
        let coin = gameData.d.gameLv[this.getGameLv()].doubleCoin + this.getTicket();
        if (seatId == this.getDZ()) {
            if (!isScore) {
                let dzCoin = this.getPlayer(this.getDZ()).getCoin();
                let nmCoin1 = this.getPlayer((this.getDZ() + 1) % 3).getCoin();
                let nmCoin2 = this.getPlayer((this.getDZ() + 2) % 3).getCoin();
                if (dzCoin < coin || nmCoin1 < coin || nmCoin2 < coin) {
                    return false;
                }
            }
            this.getPlayer(this.getDZ()).setDouble();
            this.getPlayer((this.getDZ() + 1) % 3).setDouble();
            this.getPlayer((this.getDZ() + 2) % 3).setDouble();
        } else {
            if (!isScore) {
                let dzCoin = this.getPlayer(this.getDZ()).getCoin();
                let nmCoin = this.getPlayer(seatId).getCoin();
                if (dzCoin < coin || nmCoin < coin) {
                    return false;
                }
            }
            this.getPlayer(this.getDZ()).setDouble();
            this.getPlayer(seatId).setDouble();
        }
    }
    let gameDouble = {};
    gameDouble[0] = this.getPlayer(0).isDouble();
    gameDouble[1] = this.getPlayer(1).isDouble();
    gameDouble[2] = this.getPlayer(2).isDouble();
    this.broadcast('gameDouble', {
        'local': seatId,
        'double': double,
        'doubleState': gameDouble,
        'multiples': this.getMultiples(),
    });
    return true;
};
DDZ.prototype.startPlay = function () {
    log.d('设置为地主, 传底牌给地主:', this._dzID);

    this._playID = this._dzID;
    let dz = this._players[this._dzID];
    // 设置为地主, 传底牌给地主
    dz.setDZ(this._holes);
    this._players[0].sort();
    this._players[1].sort();
    this._players[2].sort();

    save.taskUpdate(dz._pid, dz._cid, dz._uuid, 3, 1);

    this._lastHands = {'id': this._dzID, 'cards': []};
};
// 发牌
DDZ.prototype.deal = function () {
    let p = new Poker();
    let cards = p.deal();
    //log.d('aaaaaaaaaaa', cards);
    this._holes = cards[0];
    this._players[0].setCards(cards[1]);
    this._players[1].setCards(cards[2]);
    this._players[2].setCards(cards[3]);

    log.d('三张底牌:', this._holes);
};

DDZ.prototype.getStateName = function () {
    return GameState.getStateName(this._state.id);
};
DDZ.prototype.onReConnect = function (pid) {
    if (this.getStateID() == enumStatus.Playing) {

    }
}
// 出牌, 成功返回牌型信息. 失败返回错误信息
DDZ.prototype.play = function (pid, idlist) {
    if (this.getStateID() != enumStatus.Playing) {
        log.e('当前是' + this.getStateName() + '状态, 不能出牌!!');
        return false;
    }
    let id = this._playID % 3; // 0 1 2
    if (pid != null && pid != id) {
        log.d('应该由当前玩家[' + id + ']出牌');
        return 1;
    }
    if (idlist == null || idlist.length == 0) {
        if (this._lastHands != null && this._lastHands.id == id) {
            log.e("不能再PASS了", id);
            return 2;
        }
        //pass
        log.d('pass ', this._playID % 3);
        //log.d('lastHands ', this._lastHands);
        // pass后,由下家出牌. _lastHands不变
        //this._lastHands = null;
        this._playID = (this._playID + 1) % 3;
        this.broadcast('gamePlay', {
            'local': id,
            'typeId': null,
            'cards': [],
            'next': this._playID % 3
        });
        this._state.setTimeout(this);
        return {'typeId': null, 'cards': []};
    }

    let player = this._players[id];
    let idx = player.getCardsIdx(idlist);
    let cs = player.getCards(idx);
    if (cs == null)
        log.d('<<<<', player.getCardsNum());
    log.d('>>>>>>>>>>>>>>>>', id, cs);
    //cs = [new Card(3),new Card(3),new Card(3),new Card(2),new Card(1)];
    let cardsInfo = Card.parseCardType(cs);
    if (cardsInfo == null) {
        log.e('牌型错误');
        return 3;
    }
    log.d('出牌:', cardsInfo);
    if (this._lastHands != null && this._lastHands.id != id) {
        let rt = Card.compareCardType(this._lastHands, cardsInfo);
        if (!rt) {
            log.e('小于上家', cardsInfo, this._lastHands);
            return 5;
        }
        log.d('是否大于上家', rt)
    }
    // 记录出牌历史.分析春天,反春的情况.
    this._history.push(id);
    this._previousID = id;
    let gameOver = false;
    if (player.play(idx) == 0) {
        log.d('game over ', id);
        gameOver = true;
        this.setState(this.nextStateId(), id);
    }
    //if (Card.cardType.wangzha == cardsInfo.typeId) {
    //    // 如果是王炸,肯定没人要得起了. 继续由当前玩家出牌.
    //    this._lastHands = null;
    //    this._multiple *= 2;
    //}
    //else {
    //    if (Card.cardType.zhadan == cardsInfo.typeId)
    //        this._multiple *= 2;
    //    this._lastHands = cardsInfo;
    //    this._lastHands.id = id;
    //    this._players[id].setLastHands(cardsInfo);
    //    this._playID = (this._playID + 1) % 3;
    //}
    switch (cardsInfo.typeId) {
        case Card.cardType.wangzha:
            this._multiple *= 2;
            this.broadcast('gameMultiple', {
                'multiple': this.getMultiple(),
                'multiples': this.getMultiples()
            });
            save.taskUpdate(player._pid, player._cid, player._uuid, 11, 1);
            break;
        case Card.cardType.zhadan:
            this._multiple *= 2;
            this.broadcast('gameMultiple', {
                'multiple': this.getMultiple(),
                'multiples': this.getMultiples()
            });
            save.taskUpdate(player._pid, player._cid, player._uuid, 12, 1);
            break;
    }

    this._lastHands = cardsInfo;
    this._lastHands.id = id;

    this._players[id].setLastHands(cardsInfo);

    let next = null;
    if (gameOver) {
        next = -1;
    } else {
        this._playID = (this._playID + 1) % 3;
        next = this._playID % 3;
    }

    //上一家是地主1，下一家是地主2，都不是0
    this._lastHands.dz = (this._dzID==id)?1:(this._dzID==next)?2:0;

    this.broadcast('gamePlay', {
        'local': id,
        'typeId': cardsInfo.typeId,
        'cards': cardsInfo.cards,
        'next': next
    });
    this._state.setTimeout(this);
    return cardsInfo;
};
DDZ.prototype.close = function () {
    this.removeTimeout();
};

DDZ.prototype.autoRun = function () {
    switch (this.getStateID()) {
        case GameState.statusID.Bid:
            this.autoAskScore();
            break;
        case GameState.statusID.Ask:
            this.autoAskDZ();
            break;
        case GameState.statusID.Grab:
            this.autoRobDZ();
            break;
        case GameState.statusID.Playing:
            log.d('自动出牌', this._playID)
            this.autoPlay();
            break;
    }
};
// 自动为当前玩家叫分
DDZ.prototype.autoAskScore = function () {
    let id = this._playID % 3;
    let score = 0;
    if (this._players[id].isAutoPlay()) {
        score = this._players[id].askScoreLevel();
        if (score <= this._multiple) {
            score = 0;
        }
    }
    //if (this._multiple > 0 || !this._players[id].isAutoPlay()) {
    //    score = 0;
    //}
    this.askScore(id, score);
};
// 自动为当前玩家叫地主
DDZ.prototype.autoAskDZ = function () {
    let id = this._playID % 3;
    let score = 0;
    if (this._players[id].isAutoPlay()) {
        score = this._players[id].askDzLevel();
        if (score <= this._multiple) {
            score = 0;
        }
    }
    this.askDZ(id, score);
};
// 自动为当前玩家叫分
DDZ.prototype.autoRobDZ = function () {
    let id = this._dzList[this._playID % 3];
    let score = 1;
    this.robDZ(id, score);
};
// 自动为当前玩家出牌
DDZ.prototype.autoPlay = function () {
    let id = this._playID % 3;
    let player = this._players[id];
    if (this._lastHands == null || this._lastHands.id == id) {
        //出新牌
        this.play(id, player.autoPlay(null));
    }
    else { 
        //跟牌
        this.play(id, player.autoPlay(this._lastHands));
    }
};
// 添加到托管表中
DDZ.prototype.addManaged = function (seatId, auto) {
    if (this._players[seatId] == null) {
        return false;
    }
    if (auto == null) {
        if (!this._players[seatId].isAutoPlay()) {
            this._players[seatId].setAutoPlay(true);
            if (seatId == this._playID) {
                this.autoRun();
            }
        } else {
            this._players[seatId].setAutoPlay(false);
        }
    } else {
        this._players[seatId].setAutoPlay(auto);
        if (seatId == this._playID && auto == true) {
            this.autoRun();
        }
    }

    this.sendAddManaged(seatId);
};

DDZ.prototype.sendTo = function (local, action, obj) {
    if (!this._players[local]._stay) {
        return false;
    }
    if (!this._players[local]._cid || !this._players[local]._uuid) {
        return false;
    }
    msg.send(this._players[local]._cid, this._players[local]._uuid, action, obj);
};

DDZ.prototype.showCard = function (local) {
    if (this.getStateID() != enumStatus.Deal && this.getStateID() != enumStatus.Playing) {
        return false;
    }
    this._players[local].setShowCard(true);
    if (this._firstSC === null) {
        this._playID = local;
        this._firstSC = local;
        switch (this.getStateID()) {
            case enumStatus.Deal:
                this._multiple = this._multiple * 5;
                break;
            case enumStatus.Playing:
                this._multiple = this._multiple * 2;
                break;
        }
    }
    this.broadcast('gameShowCard', {
        'local': local,
        'multiple': this.getMultiple(),
        'multiples': this.getMultiples(),
        'cards': this._players[local].getHandCards()
    });
};

/**
 *
 * @param action
 * @param obj
 * @returns {boolean}
 */
DDZ.prototype.broadcast = function (action, obj) {
    let list = {};
    for (let i in this._players) {
        if (!this._players[i]._stay) {
            continue;
        }
        if (!this._players[i]._cid || !this._players[i]._uuid) {
            continue;
        }
        if (!list[this._players[i]._cid]) {

            list[this._players[i]._cid] = [];
        }
        list[this._players[i]._cid].push(this._players[i]._uuid);
    }
    for (let cid in list) {
        msg.send(cid, list[cid], action, obj);
    }
    return true;
    //for (let i in this._players) {
    //    if (!this._players[i]._stay) {
    //        continue;
    //    }
    //    if (!this._players[i]._cid || !this._players[i]._uuid) {
    //        continue;
    //    }
    //    msg.send(this._players[i]._cid, this._players[i]._uuid, action, obj);
    //}
};
DDZ.prototype.sendAskChip = function (seatId, score, end) {
    let next = this.getPlayID();
    if (end) {
        next = -1;
    }
    this.broadcast('gameAskChip', {
        'local': seatId,
        'chip': score,
        'score': score,
        'multiple': this.getMultiple(),
        'multiples': this.getMultiples(),
        'next': next
    });
};
DDZ.prototype.sendAskDZ = function (seatId, score, end) {
    let next = this.getPlayID();
    if (end) {
        next = -1;
    }
    this.broadcast('gameAskDZ', {
        'local': seatId,
        'chip': score,
        'multiple': this.getMultiple(),
        'multiples': this.getMultiples(),
        'next': next
    });
};
DDZ.prototype.sendRobDZ = function (seatId, score, end) {
    let next = this._dzList[this.getPlayID()];
    if (end) {
        next = -1;
    }
    this.broadcast('gameRobDZ', {
        'local': seatId,
        'chip': score,
        'multiple': this.getMultiple(),
        'multiples': this.getMultiples(),
        'next': next
    });
};
DDZ.prototype.sendAddManaged = function (seatId) {
    this.broadcast('gameAuto', {
        'local': seatId,
        'isAuto': this._players[seatId].isAutoPlay()
    });
};